/**
 * Created by charsen on 15/11/9.
 */
var SERVICES = angular.module("SERVICES", []);
SERVICES.factory("tools", ["$http", "$rootScope", function ($http, $rootScope) {
    return {
        /*
         * Ajax请求函数
         * */
        get: function (param) {
            if (!param instanceof Object || !param.url) {
                return;
            }
            $rootScope.$broadcast('loading');
            $http({
                method: param.method || 'GET',
                url: param.url,
                params: param.data || {},
                headers: {
                    'Accept': 'application/json, text/javascript, */*'
                }
            }).success(function (resp) {
                $rootScope.$broadcast('loading_off');
                if (resp.value == "session expire" || resp.message == 'session expire') {
                    window.location.href = '/CityExpress/html/login.html';
                }
                param.succ && param.succ(resp);
            }).error(function (resp) {
                $rootScope.$broadcast('loading_off');
                param.fail && param.fail(resp);
            });
        },
        /*
         * 清除字符串两端的空格
         * */
        trim: function (str) {
            if (!str instanceof String)
                return str;
            var res = str.replace(/^\s+|\s+$/gi, "");
            return res;
        },
        /*
         * alert指令的简便调用模式
         * */
        alert: function (title, content) {
            $rootScope.$broadcast('alert', title, content);
            return false;
        },
        /*
         * confirm指令的简便调用模式
         * */
        confirm: function (title, content) {
            $rootScope.$broadcast('confirm', title, content);
            return false;
        },
        /*
         * info指令的简便调用模式
         * */
        info: function (content) {
            $rootScope.$broadcast("info", content);
            return false;
        },
        /*
         * 检查一个对象是否为空
         * */
        isEmpty: function (obj) {
            for (var i in obj) {
                return false;
            }
            return true;
        },
        /*
         * 用于跨页面保存数据
         * */
        data: {
            data: {},
            setItem: function (key, value) {
                this.data[key] = value;
            },
            getItem: function (key) {
                return this.data[key];
            }
        },
        date: function () {
            var date = new Date();
            return "" + date.getFullYear() + ':' + date.getMonth() + ':' + date.getDate();
        },
        town: function () {
            if (/town=(.+)/gi.exec(window.location.search)) {
                var town = /town=(.+)/gi.exec(window.location.search)[1];
                if (town == 'th') {
                    return '唐河';
                } else if (town == 'sq') {
                    return '社旗';
                }
            }
        },
        towns: function () {
            if (this.town() == '唐河') {
                return ['源潭镇', '张店镇', '郭滩镇', '湖阳镇', '黑龙镇', '大河屯镇', '龙潭镇', '桐寨铺镇', '苍台镇', '上屯镇', '毕店镇', '少拜寺镇'];
            }
            if (this.town() == '社旗') {
                return ['赊店镇', '兴隆镇', '郝寨镇', '桥头镇', '李店镇', '晋庄镇', '苗店镇', '饶良镇', '青台镇', '上屯镇'];
            }
        },
        villages: function () {
            if (this.town() == '社旗') {
                return ['城郊村', '唐庄村', '陌陂村', '朱集村', '下洼村', '太和村', '丁庄村', '大冯营村', '田庄村'];
            }
            if (this.town() == '唐河') {
                return ['城郊乡', '桐河乡', '昝岗乡', '祁仪乡', '马振抚乡', '古城乡', '东王集乡'];
            }
        },
        industrys: function () {
            if (this.town() == '唐河') {
                return ['辣椒', '桐河桐蛋', '郭滩烧鸡', '唐河牛肉', '玉米', '花生', '猪肉', '羊肉', '服饰鞋帽', '水果'];
            }
            if (this.town() == '社旗') {
                return ['中草药', '水果', '粮食', '玉米', '花生', '牛肉', '猪肉', '羊肉', '服饰鞋帽', '辣椒'];
            }
        },
        get_industry_data: function () {
            if (this.town() == '唐河') {
                return [
                    {name: '辣椒', value: 66.79,},
                    {name: '桐河桐蛋', value: 46.66},
                    {name: '郭滩烧鸡', value: 40.78},
                    {name: '唐河牛肉', value: 36.99},
                    {name: '玉米', value: 30.77,},
                    {name: '花生', value: 20.66}
                ];
            }
            if (this.town() == '社旗') {
                return [
                    {name: '中草药', value: 66.79},
                    {name: '水果', value: 46.66},
                    {name: '粮食', value: 40.78},
                    {name: '玉米', value: 36.99},
                    {name: '花生', value: 30.77},
                    {name: '牛肉', value: 20.66}
                ]
            }
        },
        get_town_data: function () {
            if (this.town() == '唐河') {
                return [
                    {
                        name: '源潭镇',
                        value: 15
                    },
                    {
                        name: '张店镇',
                        value: 10
                    },
                    {
                        name: '郭滩镇',
                        value: 6
                    },
                    {
                        name: '湖阳镇',
                        value: 4
                    },
                    {
                        name: '黑龙镇',
                        value: 2
                    }
                ]
            } else if (this.town() == '社旗') {
                return [
                    {
                        name: '赊店镇',
                        value: 10
                    },
                    {
                        name: '兴隆镇',
                        value: 8
                    },
                    {
                        name: '郝寨镇',
                        value: 6
                    },
                    {
                        name: '桥头镇',
                        value: 4
                    },
                    {
                        name: '李店镇',
                        value: 3
                    }
                ]
            }
        },
        get_lat_lng_bound: function (map, salers) {
            var bs = map.getBounds();
            var bssw = bs.getSouthWest();
            var bsne = bs.getNorthEast();
            var lng_drop = Math.abs(bsne.lng - bssw.lng);
            var lat_drop = Math.abs(bsne.lat - bssw.lat);
            var points = [];
            var n = 0;
            while (true) {
                var ran_lng = Math.random() / 2;
                var ran_lat = Math.random() / 4;
                if (ran_lng < lng_drop && ran_lat < lat_drop) {
                    n++;
                    points.push({
                        lng: bssw.lng + ran_lng,
                        lat: bssw.lat + ran_lat,
                        count: salers[n][1],
                        saleCount: salers[n][2],
                        price: salers[n][3],
                        name: salers[n][0],
                        feedbackRate: 99 + Number(Math.random().toFixed(2)) + '%',
                        platform: n % 2 == 0 ? "tb" : (n % 3 == 0 ? "jd" : "tm"),
                        show: true
                    });
                    if (n == 80) {
                        break;
                    }
                }
            }
            return points;
        },
        geoCoordMap: {
            '上海': [121.4648, 31.2891],
            '东莞': [113.8953, 22.901],
            '东营': [118.7073, 37.5513],
            '中山': [113.4229, 22.478],
            '临汾': [111.4783, 36.1615],
            '临沂': [118.3118, 35.2936],
            '丹东': [124.541, 40.4242],
            '丽水': [119.5642, 28.1854],
            // '乌鲁木齐': [87.9236, 43.5883],
            '佛山': [112.8955, 23.1097],
            '保定': [115.0488, 39.0948],
            '兰州': [103.5901, 36.3043],
            '南平': [118.10, 26.38],
            '包头': [110.3467, 41.4899],
            '北京': [116.4551, 40.2539],
            '北海': [109.314, 21.6211],
            '南京': [118.8062, 31.9208],
            // '南宁': [108.479, 23.1152],
            '南昌': [116.0046, 28.6633],
            '南通': [121.1023, 32.1625],
            '厦门': [118.1689, 24.6478],
            '台州': [121.1353, 28.6688],
            '合肥': [117.29, 32.0581],
            // '呼和浩特': [111.4124, 40.4901],
            '咸阳': [108.4131, 34.8706],
            '哈尔滨': [127.9688, 45.368],
            '唐山': [118.4766, 39.6826],
            '嘉兴': [120.9155, 30.6354],
            '大庆': [125.1097, 46.5932],
            '大同': [113.7854, 39.8035],
            '大连': [122.2229, 39.4409],
            '天津': [117.4219, 39.4189],
            '太原': [112.3352, 37.9413],
            '威海': [121.9482, 37.1393],
            '宁波': [121.5967, 29.6466],
            '宝鸡': [107.1826, 34.3433],
            '宿迁': [118.5535, 33.7775],
            '常州': [119.4543, 31.5582],
            '广州': [113.5107, 23.2196],
            '廊坊': [116.521, 39.0509],
            '石嘴山': [106.3907, 38.9897],
            '辽阳': [123.2437, 41.2744],
            '信阳': [114.04, 32.07],
            '延安': [109.1052, 36.4252],
            '晋中': [112.7385, 37.6933],
            // '齐齐哈尔': [123.57, 47.20],
            '张家口': [115.1477, 40.8527],
            '阜阳': [115.48, 32.54],
            '徐州': [117.5208, 34.3268],
            '德州': [116.6858, 37.2107],
            '惠州': [114.6204, 23.1647],
            '成都': [103.9526, 30.7617],
            '扬州': [119.4653, 32.8162],
            '承德': [117.5757, 41.4075],
            // '拉萨': [91.1865, 30.1465],
            '无锡': [120.3442, 31.5527],
            '日照': [119.2786, 35.5023],
            '昆明': [102.9199, 25.4663],
            '杭州': [119.5313, 29.8773],
            '枣庄': [117.323, 34.8926],
            '柳州': [109.3799, 24.9774],
            '株洲': [113.5327, 27.0319],
            '武汉': [114.3896, 30.6628],
            '汕头': [117.1692, 23.3405],
            '河池': [108.0918, 24.6988],
            '江门': [112.6318, 22.1484],
            '沈阳': [123.1238, 42.1216],
            '沧州': [116.8286, 38.2104],
            '河源': [114.917, 23.9722],
            '泉州': [118.3228, 25.1147],
            '荆州': [112.24, 30.34],
            '洛阳': [112.4600, 34.6243],
            '泰安': [117.0264, 36.0516],
            '随州': [113.3890, 31.6963],
            '泰州': [120.0586, 32.5525],
            '济南': [117.1582, 36.8701],
            '济宁': [116.8286, 35.3375],
            '海口': [110.3893, 19.8516],
            '淄博': [118.0371, 36.6064],
            '淮安': [118.927, 33.4039],
            '深圳': [114.5435, 22.5439],
            '清远': [112.9175, 24.3292],
            '温州': [120.498, 27.8119],
            '渭南': [109.7864, 35.0299],
            '湖州': [119.8608, 30.7782],
            '湘潭': [112.5439, 27.7075],
            '滨州': [117.8174, 37.4963],
            '潍坊': [119.0918, 36.524],
            '烟台': [120.7397, 37.5128],
            '玉溪': [101.9312, 23.8898],
            '珠海': [113.7305, 22.1155],
            '普洱': [113.7305, 22.1155],
            '宜春': [114.4226, 27.8200],
            // '噶大克': [80.35, 31.733],
            // '多木拉': [82.433, 34.150],
            '盐城': [120.2234, 33.5577],
            '盘锦': [121.9482, 41.0449],
            // '亚东': [88.85, 27.417],
            // '日喀则': [88.883, 29.317],
            // '曼尼': [87.167, 34.767],
            // '拉萨': [91.167, 29.667],
            '景洪': [100.783, 21.950],
            // '潞西': [98.533, 24.40],
            // '大理': [100.167, 25.717],
            // '昆明': [102.683, 25.000],
            // '玉林': [110.150, 22.650],
            // '百色': [106.600, 23.917],
            '柳州': [109.383, 24.317],
            // '南宁': [108.333, 22.800],
            '西宁': [101.750, 36.633],
            '银川': [106.267, 38.333],
            // '苏克西': [94.133, 38.400],
            // '哈密': [93.533, 42.817],
            // '阿勒泰': [88.117, 47.917],
            // '昌吉': [87.317, 44.033],
            // '塔城': [83.000, 46.750],
            '桂林': [110.250, 25.300],
            '石家庄': [114.4995, 38.1006],
            // '改则': [85.333, 32.117],
            '福州': [119.4543, 25.9222],
            '秦皇岛': [119.2126, 40.0232],
            '绍兴': [120.564, 29.7565],
            '崇左': [107.3573, 22.4154],
            '聊城': [115.9167, 36.4032],
            '淮北': [116.7914, 33.9600],
            '肇庆': [112.1265, 23.5822],
            '舟山': [122.2559, 30.2234],
            '苏州': [120.6519, 31.3989],
            '莱芜': [117.6526, 36.2714],
            '菏泽': [115.6201, 35.2057],
            '营口': [122.4316, 40.4297],
            '葫芦岛': [120.1575, 40.578],
            '衡水': [115.8838, 37.7161],
            '驻马店': [114.0294, 33.0175],
            '南阳': [112.5350, 32.9967],
            '商丘': [115.6627, 34.4203],
            '衢州': [118.6853, 28.8666],
            '西宁': [101.4038, 36.8207],
            '西安': [109.1162, 34.2004],
            '贵阳': [106.6992, 26.7682],
            '连云港': [119.1248, 34.552],
            '邢台': [114.8071, 37.2821],
            '襄阳': [112.25, 32.22],
            '邯郸': [114.4775, 36.535],
            '郑州': [113.4668, 34.6234],
            // '鄂尔多斯': [108.9734, 39.2487],
            '重庆': [107.7539, 30.1904],
            '金华': [120.0037, 29.1028],
            '铜川': [109.0393, 35.1947],
            '银川': [106.3586, 38.1775],
            '镇江': [119.4763, 31.9702],
            '长春': [125.8154, 44.2584],
            '长沙': [113.0823, 28.2568],
            '长治': [112.8625, 36.4746],
            '阳泉': [113.4778, 38.0951],
            '十堰': [110.8045, 32.6350],
            '青岛': [120.4651, 36.3373],
            '韶关': [113.7964, 24.7028],
            '唐河': [112.867, 32.631]
        },
        sum: function (data) {
            var total = 0;
            for (var i = 0; i < data.length; i++) {
                total += data[i];
            }
            return total;
        },
        datasource: true
    }
}]);
SERVICES.factory("storage", [function () {
    var storage = {
        userData: null,
        localStorage: window.localStorage,
        init: function () {
            if (!this.localStorage) {
                try {
                    this.userData = document.createElement('INPUT');
                    this.userData.type = "hidden";
                    this.userData.style.display = "none";
                    this.userData.addBehavior("#default#userData");
                    document.body.appendChild(this.userData);
                    var expires = new Date();
                    expires.setDate(expires.getDate() + 365);
                    this.userData.expires = expires.toUTCString();
                } catch (e) {
                    return false;
                }
            }
            return true;
        },
        setItem: function (key, value) {
            if (typeof value == 'object') {
                value = JSON.stringify(value);
            }
            if (this.localStorage) {
                this.localStorage.setItem(key, value);
            } else if (this.init()) {
                this.userData.load(this.name);
                this.userData.setAttribute(key, value);
                this.userData.save(this.name);
            }
        },
        getItem: function (key) {
            var result;
            if (this.localStorage) {
                result = this.localStorage.getItem(key);
                if (/^\{.+\}$/gi.test(result)) {
                    return JSON.parse(result);
                } else {
                    return result;
                }
            } else if (this.init()) {
                this.userData.load(this.name);
                resutl = this.userData.getAttribute(key);
                if (/^\{.+\}$/gi.test(result)) {
                    return JSON.parse(result);
                } else {
                    return result;
                }
            }
        },
        removeItem: function (key) {
            if (this.localStorage) {
                this.localStorage.removeItem(key);
            } else if (this.init()) {
                this.userData.load(this.name);
                this.userData.removeAttribute(key);
                this.userData.save(this.name);
            }
        },
        random: function (Min, Max) {
            var Range = Max - Min;
            var Rand = Math.random();
            var num = Min + Math.round(Rand * Range);
            return num;
        }

    }
    return storage;
}]);